package com.unity3d.player;

import android.content.*;
import android.media.*;
import android.util.*;
import android.widget.*;
import android.view.*;

public final class m extends FrameLayout implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer$OnVideoSizeChangedListener, SurfaceHolder$Callback, MediaController$MediaPlayerControl
{
    private static boolean a = false;
    private final Context b;
    private final SurfaceView c;
    private final SurfaceHolder d;
    private final String e;
    private final int f;
    private final int g;
    private final boolean h;
    private final long i;
    private final long j;
    private final FrameLayout k;
    private final Display l;
    private int m;
    private int n;
    private int o;
    private int p;
    private MediaPlayer q;
    private MediaController r;
    private boolean s;
    private boolean t;
    private int u;
    private boolean v;
    private boolean w;
    private a x;
    private b y;
    private volatile int z;

    protected m(final Context b, final String e, final int backgroundColor, final int f, final int g, final boolean h, final long i, final long j, final a x) {
        super(b);
        this.s = false;
        this.t = false;
        this.u = 0;
        this.v = false;
        this.w = false;
        this.z = 0;
        this.x = x;
        this.b = b;
        this.k = this;
        this.c = new SurfaceView(b);
        (this.d = this.c.getHolder()).addCallback((SurfaceHolder.Callback)this);
        this.d.setType(3);
        this.k.setBackgroundColor(backgroundColor);
        this.k.addView((View)this.c);
        this.l = ((WindowManager)this.b.getSystemService("window")).getDefaultDisplay();
        this.e = e;
        this.f = f;
        this.g = g;
        this.h = h;
        this.i = i;
        this.j = j;
        if (com.unity3d.player.m.a) {
            final StringBuilder sb = new StringBuilder("fileName: ");
            sb.append(this.e);
            b(sb.toString());
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb2 = new StringBuilder("backgroundColor: ");
            sb2.append(backgroundColor);
            b(sb2.toString());
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb3 = new StringBuilder("controlMode: ");
            sb3.append(this.f);
            b(sb3.toString());
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb4 = new StringBuilder("scalingMode: ");
            sb4.append(this.g);
            b(sb4.toString());
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb5 = new StringBuilder("isURL: ");
            sb5.append(this.h);
            b(sb5.toString());
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb6 = new StringBuilder("videoOffset: ");
            sb6.append(this.i);
            b(sb6.toString());
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb7 = new StringBuilder("videoLength: ");
            sb7.append(this.j);
            b(sb7.toString());
        }
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
    }

    private void a(final int z) {
        this.z = z;
        final a x = this.x;
        if (x != null) {
            x.a(this.z);
        }
    }

    private static void b(final String s) {
        final StringBuilder sb = new StringBuilder("VideoPlayer: ");
        sb.append(s);
        Log.i("Video", sb.toString());
    }

    private void c() {
        //
        // This method could not be decompiled.
        //
        // Original Bytecode:
        //
        //     1: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //     4: astore_1
        //     5: aload_1
        //     6: ifnull          43
        //     9: aload_1
        //    10: aload_0
        //    11: getfield        com/unity3d/player/m.d:Landroid/view/SurfaceHolder;
        //    14: invokevirtual   android/media/MediaPlayer.setDisplay:(Landroid/view/SurfaceHolder;)V
        //    17: aload_0
        //    18: getfield        com/unity3d/player/m.v:Z
        //    21: ifne            42
        //    24: getstatic       com/unity3d/player/m.a:Z
        //    27: ifeq            35
        //    30: ldc             "Resuming playback"
        //    32: invokestatic    com/unity3d/player/m.b:(Ljava/lang/String;)V
        //    35: aload_0
        //    36: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //    39: invokevirtual   android/media/MediaPlayer.start:()V
        //    42: return
        //    43: aload_0
        //    44: iconst_0
        //    45: invokespecial   com/unity3d/player/m.a:(I)V
        //    48: aload_0
        //    49: invokevirtual   com/unity3d/player/m.doCleanUp:()V
        //    52: aload_0
        //    53: new             Landroid/media/MediaPlayer;
        //    56: dup
        //    57: invokespecial   android/media/MediaPlayer.<init>:()V
        //    60: putfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //    63: aload_0
        //    64: getfield        com/unity3d/player/m.h:Z
        //    67: ifeq            91
        //    70: aload_0
        //    71: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //    74: aload_0
        //    75: getfield        com/unity3d/player/m.b:Landroid/content/Context;
        //    78: aload_0
        //    79: getfield        com/unity3d/player/m.e:Ljava/lang/String;
        //    82: invokestatic    android/net/Uri.parse:(Ljava/lang/String;)Landroid/net/Uri;
        //    85: invokevirtual   android/media/MediaPlayer.setDataSource:(Landroid/content/Context;Landroid/net/Uri;)V
        //    88: goto            207
        //    91: aload_0
        //    92: getfield        com/unity3d/player/m.j:J
        //    95: lconst_0
        //    96: lcmp
        //    97: ifeq            138
        //   100: new             Ljava/io/FileInputStream;
        //   103: dup
        //   104: aload_0
        //   105: getfield        com/unity3d/player/m.e:Ljava/lang/String;
        //   108: invokespecial   java/io/FileInputStream.<init>:(Ljava/lang/String;)V
        //   111: astore_1
        //   112: aload_0
        //   113: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   116: aload_1
        //   117: invokevirtual   java/io/FileInputStream.getFD:()Ljava/io/FileDescriptor;
        //   120: aload_0
        //   121: getfield        com/unity3d/player/m.i:J
        //   124: aload_0
        //   125: getfield        com/unity3d/player/m.j:J
        //   128: invokevirtual   android/media/MediaPlayer.setDataSource:(Ljava/io/FileDescriptor;JJ)V
        //   131: aload_1
        //   132: invokevirtual   java/io/FileInputStream.close:()V
        //   135: goto            207
        //   138: aload_0
        //   139: invokevirtual   com/unity3d/player/m.getResources:()Landroid/content/res/Resources;
        //   142: invokevirtual   android/content/res/Resources.getAssets:()Landroid/content/res/AssetManager;
        //   145: astore_1
        //   146: aload_1
        //   147: aload_0
        //   148: getfield        com/unity3d/player/m.e:Ljava/lang/String;
        //   151: invokevirtual   android/content/res/AssetManager.openFd:(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;
        //   154: astore_1
        //   155: aload_0
        //   156: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   159: aload_1
        //   160: invokevirtual   android/content/res/AssetFileDescriptor.getFileDescriptor:()Ljava/io/FileDescriptor;
        //   163: aload_1
        //   164: invokevirtual   android/content/res/AssetFileDescriptor.getStartOffset:()J
        //   167: aload_1
        //   168: invokevirtual   android/content/res/AssetFileDescriptor.getLength:()J
        //   171: invokevirtual   android/media/MediaPlayer.setDataSource:(Ljava/io/FileDescriptor;JJ)V
        //   174: aload_1
        //   175: invokevirtual   android/content/res/AssetFileDescriptor.close:()V
        //   178: goto            207
        //   181: new             Ljava/io/FileInputStream;
        //   184: dup
        //   185: aload_0
        //   186: getfield        com/unity3d/player/m.e:Ljava/lang/String;
        //   189: invokespecial   java/io/FileInputStream.<init>:(Ljava/lang/String;)V
        //   192: astore_1
        //   193: aload_0
        //   194: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   197: aload_1
        //   198: invokevirtual   java/io/FileInputStream.getFD:()Ljava/io/FileDescriptor;
        //   201: invokevirtual   android/media/MediaPlayer.setDataSource:(Ljava/io/FileDescriptor;)V
        //   204: goto            131
        //   207: aload_0
        //   208: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   211: aload_0
        //   212: getfield        com/unity3d/player/m.d:Landroid/view/SurfaceHolder;
        //   215: invokevirtual   android/media/MediaPlayer.setDisplay:(Landroid/view/SurfaceHolder;)V
        //   218: aload_0
        //   219: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   222: iconst_1
        //   223: invokevirtual   android/media/MediaPlayer.setScreenOnWhilePlaying:(Z)V
        //   226: aload_0
        //   227: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   230: aload_0
        //   231: invokevirtual   android/media/MediaPlayer.setOnBufferingUpdateListener:(Landroid/media/MediaPlayer$OnBufferingUpdateListener;)V
        //   234: aload_0
        //   235: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   238: aload_0
        //   239: invokevirtual   android/media/MediaPlayer.setOnCompletionListener:(Landroid/media/MediaPlayer$OnCompletionListener;)V
        //   242: aload_0
        //   243: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   246: aload_0
        //   247: invokevirtual   android/media/MediaPlayer.setOnPreparedListener:(Landroid/media/MediaPlayer$OnPreparedListener;)V
        //   250: aload_0
        //   251: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   254: aload_0
        //   255: invokevirtual   android/media/MediaPlayer.setOnVideoSizeChangedListener:(Landroid/media/MediaPlayer$OnVideoSizeChangedListener;)V
        //   258: aload_0
        //   259: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   262: iconst_3
        //   263: invokevirtual   android/media/MediaPlayer.setAudioStreamType:(I)V
        //   266: aload_0
        //   267: getfield        com/unity3d/player/m.q:Landroid/media/MediaPlayer;
        //   270: invokevirtual   android/media/MediaPlayer.prepareAsync:()V
        //   273: aload_0
        //   274: new             Lcom/unity3d/player/m$b;
        //   277: dup
        //   278: aload_0
        //   279: aload_0
        //   280: invokespecial   com/unity3d/player/m$b.<init>:(Lcom/unity3d/player/m;Lcom/unity3d/player/m;)V
        //   283: putfield        com/unity3d/player/m.y:Lcom/unity3d/player/m$b;
        //   286: new             Ljava/lang/Thread;
        //   289: dup
        //   290: aload_0
        //   291: getfield        com/unity3d/player/m.y:Lcom/unity3d/player/m$b;
        //   294: invokespecial   java/lang/Thread.<init>:(Ljava/lang/Runnable;)V
        //   297: invokevirtual   java/lang/Thread.start:()V
        //   300: return
        //   301: astore_1
        //   302: getstatic       com/unity3d/player/m.a:Z
        //   305: ifeq            341
        //   308: new             Ljava/lang/StringBuilder;
        //   311: dup
        //   312: ldc_w           "error: "
        //   315: invokespecial   java/lang/StringBuilder.<init>:(Ljava/lang/String;)V
        //   318: astore_2
        //   319: aload_2
        //   320: aload_1
        //   321: invokevirtual   java/lang/Exception.getMessage:()Ljava/lang/String;
        //   324: invokevirtual   java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        //   327: pop
        //   328: aload_2
        //   329: aload_1
        //   330: invokevirtual   java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
        //   333: pop
        //   334: aload_2
        //   335: invokevirtual   java/lang/StringBuilder.toString:()Ljava/lang/String;
        //   338: invokestatic    com/unity3d/player/m.b:(Ljava/lang/String;)V
        //   341: aload_0
        //   342: iconst_2
        //   343: invokespecial   com/unity3d/player/m.a:(I)V
        //   346: return
        //   347: astore_1
        //   348: goto            181
        //    Exceptions:
        //  Try           Handler
        //  Start  End    Start  End    Type
        //  -----  -----  -----  -----  ---------------------
        //  52     88     301    347    Ljava/lang/Exception;
        //  91     131    301    347    Ljava/lang/Exception;
        //  131    135    301    347    Ljava/lang/Exception;
        //  138    146    301    347    Ljava/lang/Exception;
        //  146    178    347    207    Ljava/io/IOException;
        //  146    178    301    347    Ljava/lang/Exception;
        //  181    204    301    347    Ljava/lang/Exception;
        //  207    300    301    347    Ljava/lang/Exception;
        //
        // The error that occurred was:
        //
        // java.lang.IllegalStateException: Expression is linked from several locations: Label_0181:
        //     at com.strobel.decompiler.ast.Error.expressionLinkedFromMultipleLocations(Error.java:27)
        //     at com.strobel.decompiler.ast.AstOptimizer.mergeDisparateObjectInitializations(AstOptimizer.java:2596)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:235)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:42)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:214)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:99)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethodBody(AstBuilder.java:782)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethod(AstBuilder.java:675)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addTypeMembers(AstBuilder.java:552)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeCore(AstBuilder.java:519)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeNoCache(AstBuilder.java:161)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createType(AstBuilder.java:150)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addType(AstBuilder.java:125)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.buildAst(JavaLanguage.java:71)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.decompileType(JavaLanguage.java:59)
        //     at us.deathmarine.luyten.DecompilerLinkProvider.generateContent(DecompilerLinkProvider.java:97)
        //     at us.deathmarine.luyten.OpenFile.decompileWithNavigationLinks(OpenFile.java:494)
        //     at us.deathmarine.luyten.OpenFile.decompile(OpenFile.java:467)
        //     at us.deathmarine.luyten.Model.extractClassToTextPane(Model.java:420)
        //     at us.deathmarine.luyten.Model.openEntryByTreePath(Model.java:339)
        //     at us.deathmarine.luyten.Model$TreeListener$1.run(Model.java:266)
        //
        throw new IllegalStateException("An error occurred while decompiling this method.");
    }

    private void d() {
        if (this.isPlaying()) {
            return;
        }
        this.a(1);
        if (com.unity3d.player.m.a) {
            b("startVideoPlayback");
        }
        this.updateVideoLayout();
        if (!this.v) {
            this.start();
        }
    }

    public final void CancelOnPrepare() {
        this.a(2);
    }

    final boolean a() {
        return this.v;
    }

    public final boolean canPause() {
        return true;
    }

    public final boolean canSeekBackward() {
        return true;
    }

    public final boolean canSeekForward() {
        return true;
    }

    protected final void destroyPlayer() {
        if (com.unity3d.player.m.a) {
            b("destroyPlayer");
        }
        if (!this.v) {
            this.pause();
        }
        this.doCleanUp();
    }

    protected final void doCleanUp() {
        final b y = this.y;
        if (y != null) {
            y.a();
            this.y = null;
        }
        final MediaPlayer q = this.q;
        if (q != null) {
            q.release();
            this.q = null;
        }
        this.o = 0;
        this.p = 0;
        this.t = false;
        this.s = false;
    }

    public final int getBufferPercentage() {
        if (this.h) {
            return this.u;
        }
        return 100;
    }

    public final int getCurrentPosition() {
        final MediaPlayer q = this.q;
        if (q == null) {
            return 0;
        }
        return q.getCurrentPosition();
    }

    public final int getDuration() {
        final MediaPlayer q = this.q;
        if (q == null) {
            return 0;
        }
        return q.getDuration();
    }

    public final boolean isPlaying() {
        final boolean b = this.t && this.s;
        final MediaPlayer q = this.q;
        if (q == null) {
            return !b;
        }
        return q.isPlaying() || !b;
    }

    public final void onBufferingUpdate(final MediaPlayer mediaPlayer, final int u) {
        if (com.unity3d.player.m.a) {
            final StringBuilder sb = new StringBuilder("onBufferingUpdate percent:");
            sb.append(u);
            b(sb.toString());
        }
        this.u = u;
    }

    public final void onCompletion(final MediaPlayer mediaPlayer) {
        if (com.unity3d.player.m.a) {
            b("onCompletion called");
        }
        this.destroyPlayer();
        this.a(3);
    }

    public final boolean onKeyDown(final int n, final KeyEvent keyEvent) {
        if (n == 4 || (this.f == 2 && n != 0 && !keyEvent.isSystem())) {
            this.destroyPlayer();
            this.a(3);
            return true;
        }
        final MediaController r = this.r;
        if (r != null) {
            return r.onKeyDown(n, keyEvent);
        }
        return super.onKeyDown(n, keyEvent);
    }

    public final void onPrepared(final MediaPlayer mediaPlayer) {
        if (com.unity3d.player.m.a) {
            b("onPrepared called");
        }
        final b y = this.y;
        if (y != null) {
            y.a();
            this.y = null;
        }
        final int f = this.f;
        if (f == 0 || f == 1) {
            (this.r = new MediaController(this.b)).setMediaPlayer((MediaController.MediaPlayerControl)this);
            this.r.setAnchorView((View)this);
            this.r.setEnabled(true);
            this.r.show();
        }
        this.t = true;
        if (this.t && this.s) {
            this.d();
        }
    }

    public final boolean onTouchEvent(final MotionEvent motionEvent) {
        final int action = motionEvent.getAction();
        if (this.f == 2 && (action & 0xFF) == 0x0) {
            this.destroyPlayer();
            this.a(3);
            return true;
        }
        final MediaController r = this.r;
        if (r != null) {
            return r.onTouchEvent(motionEvent);
        }
        return super.onTouchEvent(motionEvent);
    }

    public final void onVideoSizeChanged(final MediaPlayer mediaPlayer, final int o, final int p3) {
        if (com.unity3d.player.m.a) {
            final StringBuilder sb = new StringBuilder("onVideoSizeChanged called ");
            sb.append(o);
            sb.append("x");
            sb.append(p3);
            b(sb.toString());
        }
        if (o != 0 && p3 != 0) {
            this.s = true;
            this.o = o;
            this.p = p3;
            if (this.t && this.s) {
                this.d();
            }
            return;
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb2 = new StringBuilder("invalid video width(");
            sb2.append(o);
            sb2.append(") or height(");
            sb2.append(p3);
            sb2.append(")");
            b(sb2.toString());
        }
    }

    public final void pause() {
        final MediaPlayer q = this.q;
        if (q == null) {
            return;
        }
        if (this.w) {
            q.pause();
        }
        this.v = true;
    }

    public final void seekTo(final int n) {
        final MediaPlayer q = this.q;
        if (q == null) {
            return;
        }
        q.seekTo(n);
    }

    public final void start() {
        if (com.unity3d.player.m.a) {
            b("Start");
        }
        final MediaPlayer q = this.q;
        if (q == null) {
            return;
        }
        if (this.w) {
            q.start();
        }
        this.v = false;
    }

    public final void surfaceChanged(final SurfaceHolder surfaceHolder, final int n, final int m, final int n2) {
        if (m.a) {
            final StringBuilder sb = new StringBuilder("surfaceChanged called ");
            sb.append(n);
            sb.append(" ");
            sb.append(m);
            sb.append("x");
            sb.append(n2);
            b(sb.toString());
        }
        if (this.m != m || this.n != n2) {
            this.m = m;
            this.n = n2;
            if (this.w) {
                this.updateVideoLayout();
            }
        }
    }

    public final void surfaceCreated(final SurfaceHolder surfaceHolder) {
        if (com.unity3d.player.m.a) {
            b("surfaceCreated called");
        }
        this.w = true;
        this.c();
    }

    public final void surfaceDestroyed(final SurfaceHolder surfaceHolder) {
        if (com.unity3d.player.m.a) {
            b("surfaceDestroyed called");
        }
        this.w = false;
    }

    protected final void updateVideoLayout() {
        if (com.unity3d.player.m.a) {
            b("updateVideoLayout");
        }
        if (this.q == null) {
            return;
        }
        if (this.m == 0 || this.n == 0) {
            final WindowManager windowManager = (WindowManager)this.b.getSystemService("window");
            this.m = windowManager.getDefaultDisplay().getWidth();
            this.n = windowManager.getDefaultDisplay().getHeight();
        }
        int m = this.m;
        int n = this.n;
        final int o = this.o;
        final float n2 = (float)o;
        final int p = this.p;
        final float n3 = n2 / p;
        final float n4 = m / (float)n;
        final int g = this.g;
        Label_0181: {
            Label_0144: {
                if (g == 1) {
                    if (n4 > n3) {
                        break Label_0144;
                    }
                }
                else if (g == 2) {
                    if (n4 < n3) {
                        break Label_0144;
                    }
                }
                else {
                    if (g == 0) {
                        m = o;
                        n = p;
                    }
                    break Label_0181;
                }
                n = (int)(m / n3);
                break Label_0181;
            }
            m = (int)(n * n3);
        }
        if (com.unity3d.player.m.a) {
            final StringBuilder sb = new StringBuilder("frameWidth = ");
            sb.append(m);
            sb.append("; frameHeight = ");
            sb.append(n);
            b(sb.toString());
        }
        this.k.updateViewLayout((View)this.c, (ViewGroup.LayoutParams)new FrameLayout.LayoutParams(m, n, 17));
    }

    public interface a
    {
        void a(final int p0);
    }

    public final class b implements Runnable
    {
        private m b;
        private boolean c;

        public b(final m b) {
            this.b = b;
            this.c = false;
        }

        public final void a() {
            this.c = true;
        }

        @Override
        public final void run() {
            Label_0009_Outer:
            while (true) {
                while (true) {
                    try {
                        Thread.sleep(5000L);
                        while (true) {
                            if (!this.c) {
                                if (com.unity3d.player.m.a) {
                                    b("Stopping the video player due to timeout.");
                                }
                                this.b.CancelOnPrepare();
                            }
                            return;
                            Thread.currentThread().interrupt();
                            continue Label_0009_Outer;
                        }
                    }
                    catch (InterruptedException ex) {}
                    continue;
                }
            }
        }
    }
}
